#ifndef __SDEXT_DEVICE__H
#define __SDEXT_DEVICE__H

#include "StdAfx.h"
#include <windows.h>
//#include <winioctl.h>
#include <sffdisk.h>
#include <sddef.h>


const int SDCMD_SEND_CSD =  9;
class  SdManager
{
public:
	BOOL CheckSDCard(const wchar_t* drive);

private:
	class CommandData
	{
		#pragma warning(disable:4200)
		struct SDBuffer {
			char dcd[sizeof(SFFDISK_DEVICE_COMMAND_DATA)];
			SDCMD_DESCRIPTOR Descriptor;
			BYTE Buffer[0];
		} *data;

		UINT length;

	public:
		BOOL Execute(const wchar_t* drive, LPDWORD pReturnBytes = NULL);
		CommandData(ULONG BufferSize);
		virtual ~CommandData();

		void SetCommandParameter(
			SD_COMMAND_CODE Cmd,
			SD_COMMAND_CLASS CmdClass,
			SD_TRANSFER_DIRECTION TransferDirection,
			SD_TRANSFER_TYPE TransferType,
			SD_RESPONSE_TYPE ResponseType
		);
		SFFDISK_DEVICE_COMMAND_DATA &GetCommandData() {return *(SFFDISK_DEVICE_COMMAND_DATA *)data->dcd;}
		PBYTE GetBuffer() {return data->Buffer;}
		LPBYTE GetBuffer() const {return data->Buffer;}
		UINT GetBufferSize() {return GetCommandData().DeviceDataBufferSize;}
		UINT GetTotalSize() {return length;}
		SDCMD_DESCRIPTOR &GetDescriptor() {return data->Descriptor;}
		SDCMD_DESCRIPTOR *operator->() {return &data->Descriptor;}
	};

};
#endif
